package servlet;

import service.IAdminService;
import service.imp.AdminService;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Consumer;

public class AdminServlet extends javax.servlet.http.HttpServlet {
    private IAdminService service = new AdminService();
    private Map<String, Consumer<AdminServletMethods> > methods;
    protected void doPost(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, IOException {
        doGet(request, response);
    }


    protected void doGet(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, IOException {
        // 修改编码
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("utf-8");
        response.setContentType("application/json;charset=utf-8");
        // 获得method 关键字, 作为执行函数的key
        String method = request.getParameter("method");
        // 创建AdminServletMethods对象
        AdminServletMethods md = new AdminServletMethods(response, request, service);
        // 初始化映射表
        initMapping();
        // 根据请求接口的 method 作为 key, 来执行映射表中对应的函数
        // 设置Default为 key值为 other 时 映射的函数
        methods.getOrDefault(method, methods.get("other")).accept(md);
    }

    private void initMapping() {
        // 初始化函数映射表
        // put的两个参数: key, AdminServletMethods的函数方法
        methods = new HashMap<>();
        methods.put("getHome", AdminServletMethods::getHome);
        methods.put("getUser", AdminServletMethods::getUser);
        methods.put("login", AdminServletMethods::doLogin);
        methods.put("getUserInfo", AdminServletMethods::getUserInfo);
        methods.put("updateUser", AdminServletMethods::updateUser);
        methods.put("deleteUser", AdminServletMethods::deleteUser);
        methods.put("register", AdminServletMethods::doRegister);
        methods.put("outLogin", AdminServletMethods::outLogin);
        methods.put("other", AdminServletMethods::getDefault);
        methods.put("getProduct", AdminServletMethods::getProduct);
        methods.put("updateProduct", AdminServletMethods::updateProduct);
        methods.put("deleteProduct", AdminServletMethods::deleteProduct);
        methods.put("addProduct", AdminServletMethods::addProduct);
        methods.put("getProductnote", AdminServletMethods::getProductnote);
        methods.put("deleteProductnote", AdminServletMethods::deleteProductnote);
        methods.put("addProductnote", AdminServletMethods::addProductnote);
        methods.put("updateProductnote", AdminServletMethods::updateProductnote);
        methods.put("deleteProductnote", AdminServletMethods::deleteProductnote);
        methods.put("addPurchasedProduct", AdminServletMethods::addPurchasedProduct);
        methods.put("deletPurchasedProduct", AdminServletMethods::deletPurchasedProduct);
        methods.put("getPurchasedProduct", AdminServletMethods::getPurchasedProduct);
    }
}
